package com.ruoyi.fsepc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.fsepc.domain.StPlanDetails;
import com.ruoyi.fsepc.service.IStPlanDetailsService;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工程师FSE计划详情 工程师FSE计划详情Controller
 *
 * @author ruoyi
 * @date 2020-02-28
 */
@Controller
@RequestMapping("/fsepc/details")
public class StPlanDetailsController extends BaseController {
    private String prefix = "fsepc/details";

    @Autowired
    private IStPlanDetailsService stPlanDetailsService;

    @RequiresPermissions("fsepc:details:view")
    @GetMapping()
    public String details(ModelMap mmap) {
        SysUser user = ShiroUtils.getSysUser();
        Long roleId = user.getRoles().get(0).getRoleId();
        mmap.put("roleId", roleId);
        mmap.put("tlId", StringUtils.isEmpty(user.getTeamleaderId()) ? "" : user.getTeamleaderId());
        mmap.put("fseId", null == user.getEnginnerId() ? "" : user.getEnginnerId());
        return prefix + "/details";
    }

    /**
     * 查询工程师FSE计划详情 工程师FSE计划详情列表
     */
    @RequiresPermissions("fsepc:details:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StPlanDetails stPlanDetails) {
        startPage();
        List<StPlanDetails> list = stPlanDetailsService.selectStPlanDetailsList(stPlanDetails);
        return getDataTable(list);
    }

    /**
     * 导出工程师FSE计划详情 工程师FSE计划详情列表
     */
    @RequiresPermissions("fsepc:details:export")
    @Log(title = "工程师FSE计划详情 工程师FSE计划详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StPlanDetails stPlanDetails) {
        List<StPlanDetails> list = new ArrayList<StPlanDetails>();
        List<Map<String, Object>> maplist = stPlanDetailsService.selectFseWorkPlan(stPlanDetails);
        for (Map<String ,Object> objectMap:maplist){
            stPlanDetails=new StPlanDetails();
            String begintime="";
            String endtime="";
            for(String s:objectMap.keySet()){
                if(s.equals("estimatedDepartureTime")){
                    stPlanDetails.setEstimatedDepartureTime((Date) objectMap.get(s));
                }
                if (s.equals("estimatedVisitingTime")){
                    stPlanDetails.setEstimatedVisitingTime((Date) objectMap.get(s));
                }
                if(s.equals("countNum")){
                    stPlanDetails.setCountNum(objectMap.get(s)+"");
                }
                if(s.equals("customerName")){
                    stPlanDetails.setCustomerName(objectMap.get(s)+"");
                }
                if(s.equals("fseName")){
                    stPlanDetails.setFseName(objectMap.get(s)+"");
                }

            }
            /* stPlanDetails.setTime(begintime+endtime);*/
            list.add(stPlanDetails);

        }
        ExcelUtil<StPlanDetails> util = new ExcelUtil<StPlanDetails>(StPlanDetails.class);
        return util.exportExcel(list, "details");
    }

    /**
     * 新增工程师FSE计划详情 工程师FSE计划详情
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存工程师FSE计划详情 工程师FSE计划详情
     */
    @RequiresPermissions("fsepc:details:add")
    @Log(title = "工程师FSE计划详情 工程师FSE计划详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StPlanDetails stPlanDetails) {
        return toAjax(stPlanDetailsService.insertStPlanDetails(stPlanDetails));
    }

    /**
     * 修改工程师FSE计划详情 工程师FSE计划详情
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        StPlanDetails stPlanDetails = stPlanDetailsService.selectStPlanDetailsById(id);
        mmap.put("stPlanDetails", stPlanDetails);
        return prefix + "/edit";
    }

    /**
     * 修改保存工程师FSE计划详情 工程师FSE计划详情
     */
    @RequiresPermissions("fsepc:details:edit")
    @Log(title = "工程师FSE计划详情 工程师FSE计划详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StPlanDetails stPlanDetails) {
        return toAjax(stPlanDetailsService.updateStPlanDetails(stPlanDetails));
    }

    /**
     * 删除工程师FSE计划详情 工程师FSE计划详情
     */
    @RequiresPermissions("fsepc:details:remove")
    @Log(title = "工程师FSE计划详情 工程师FSE计划详情", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(stPlanDetailsService.deleteStPlanDetailsByIds(ids));
    }

    /**
     * 查询工程师工作计划
     *
     * @param stPlanDetails
     * @return
     */
    @PostMapping("/selectFseWorkPlan")
    @ResponseBody
    public TableDataInfo selectFseWorkPlan(StPlanDetails stPlanDetails) {
        startPage();
        List<Map<String, Object>> list = stPlanDetailsService.selectFseWorkPlan(stPlanDetails);
        return getDataTable(list);
    }

    /**
     * 查询工程师工作计划
     *
     * @param stPlanDetails
     * @return
     */
    @PostMapping("/selectFseWorkPlanPC")
    @ResponseBody
    public TableDataInfo selectFseWorkPlanPC(StPlanDetails stPlanDetails) {
        startPage();
        List<Map<String, Object>> list = stPlanDetailsService.selectFseWorkPlanPC(stPlanDetails);
        return getDataTable(list);
    }
}
